package cn.stard.basic.service.impl;


import cn.stard.basic.mapper.BaseMapper;
import cn.stard.basic.query.BaseQuery;
import cn.stard.basic.service.IBaseService;
import cn.stard.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 一定不要在公共实现加上@Service注解，因为我们并不需要创建公共实现类的Bean对象
// 事务可以继承过去，所以我们直接在公共的实现类中加上事务注解
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {
        // 1.获取到分页总条数
        Integer total = baseMapper.queryCount(baseQuery);

        // 3.获取到分页数据
        List<T> rows = baseMapper.queryData(baseQuery);

        // 4.返回分页对象
        return new PageList<T>(total, rows);
    }

    @Override
    public Integer queryCount(BaseQuery baseQuery) {
        return baseMapper.queryCount(baseQuery);
    }

    @Override
    public void deles(List<Long> ids) {
        baseMapper.deles(ids);
    }



    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    @Transactional
    public void update(T t) {

        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }



    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
}